package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.model.pojo.Report;
import com.tanhua.dubbo.mapper.ReportMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class ReportApiImpl implements ReportApi{

    @Autowired
    private ReportMapper reportMapper;

    @Override
    public Long save(Report report) {
        reportMapper.insert(report);
        return report.getId();
    }

    @Override
    public Report findById(Long id) {
        return reportMapper.selectById(id);
    }

    @Override
    public List<Report> findByCharacter(Long id) {
        return reportMapper.selectList(Wrappers.lambdaQuery(Report.class)
                .eq(Report::getCharacterId, id));
    }

    @Override
    public void update(Report report) {
        reportMapper.updateById(report);
    }

    @Override
    public Report findByUserIdAndQuestionnaireId(Long userId, Long questionnaireId) {
        return reportMapper.selectOne(
                Wrappers.lambdaQuery(Report.class)
                        .eq(Report::getUserId, userId)
                        .eq(Report::getQuestionnaireId, questionnaireId)
        );
    }
}
